package businesslogic.userBl;

import java.io.Serializable;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.util.ArrayList;

import po.userPO.StudentPO;
import vo.userVO.StudentVO;
import businesslogicservice.userblservice.StudentBLService;
import dataservice.userDataService.UserDatabaseService;

public class Student extends User implements StudentBLService, Serializable {
	private static final String ADDRESS_STRING = "localhost";

	public Student(String UserID) {
		super(UserID);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 7169236962258961532L;

	@Override
	public double getGPA(int term) throws Exception {

		UserDatabaseService userDatabaseService;

		if (System.getSecurityManager() == null)
			System.setSecurityManager(new RMISecurityManager());
		try {

			userDatabaseService = (UserDatabaseService) Naming.lookup("rmi://"
					+ ADDRESS_STRING + ":8888/userData");

			StudentPO student = (StudentPO) userDatabaseService.find(UserID);

			double gpa = student.getGPA(term);

			return gpa;

		} catch (Exception e) {

			e.printStackTrace();
			throw e;
		}

	}

	public StudentVO getStudentVO() throws Exception {
		UserDatabaseService userDatabaseService;

//		if (System.getSecurityManager() == null)
//			System.setSecurityManager(new RMISecurityManager());
		try {

			userDatabaseService = (UserDatabaseService) Naming.lookup("rmi://"
					+ ADDRESS_STRING + ":8888/userData");

			StudentPO findResult = (StudentPO) userDatabaseService.find(UserID);

			StudentVO student = new StudentVO(findResult.getId(),
					findResult.getName(), findResult.getDepartmentId(),
					findResult.getGrade());

			return student;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}

	}

	@Override
	public ArrayList<StudentVO> findStudentByTermAndDepartment(int term,
			String department) throws Exception {
		UserDatabaseService userDatabaseService;
		try {

			userDatabaseService = (UserDatabaseService) Naming.lookup("rmi://"
					+ ADDRESS_STRING + ":8888/userData");
			ArrayList<StudentPO> studentPOs=userDatabaseService.findStudentPOByTerm(term, department);
			ArrayList<StudentVO> studentVOs=new ArrayList<>();
			for (StudentPO findResult:studentPOs){
				StudentVO student = new StudentVO(findResult.getId(),
						findResult.getName(), findResult.getDepartmentId(),
						findResult.getGrade());
				studentVOs.add(student);
			}
			return studentVOs;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

}
